<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class UserController extends Controller {
    
    //用户修改
    public function updateUser(){
    	if(session('name')){
    		$userTable = D('User');
    		$map['id'] = I('param.id'); 
    		$data=array();
    		if(!empty(I('param.pwd',''))){
    			$data['pwd'] = I('param.pwd','');
    		}
    		if(!empty(I('param.type',''))){
    			$data['type'] = I('param.type','');
    		}
    		if(!empty(I('param.email',''))){
    			$data['email'] = I('param.email','');
    		}
    		if(!empty(I('param.alert_type',''))){
    			$data['alert_type'] = I('param.alert_type','');
    		}
    		if(I('param.lock','') !=null){
    			$data['lock'] = I('param.lock','');
    		}    	
    		$userTable->where($map)->save($data);
    		echo json_encode(array('result'=>'修改成功','code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //用户添加
    public function addUser(){
    	if(session('name')){
    		$userTable = D('User');
    		$data['name'] = I('param.name','');
    		$data['pwd'] = I('param.pwd','');
    		$data['email'] = I('param.email','');
    		if($userTable->create($data)){
    			$map['name'] = $data['name'];
    			$user = $userTable->where($map)->select();
    			if($user){
    				echo json_encode(array('result'=>'用户名重复','code'=>0));
    				exit();
    			}
    			$userTable->add($data);
    			echo json_encode(array('result'=>'添加成功','code'=>1));
    		}else{
    			echo json_encode(array('result'=>$userTable->getError(),'code'=>0));
    		}
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    //用户删除
    public function deleteUser(){
    	if(session('name')){
    		$userTable = D('User');
    		$userTable->delete(I('param.id'));
    		echo json_encode(array('result'=>'删除成功','code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //用户list
    public function userList(){
    	if(session('name')){
    		$userTable = D('User');
    		if(!empty(I('param.page','')) && !empty(I('param.pageCount',''))){
    			$userTable->page(I('param.page'),I('param.pageCount'));
    		}
    		$user = $userTable->select();
    		echo json_encode(array('result'=>$user,'count'=>$userTable->count(),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    

}